﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Security.Cryptography;

namespace ZWZ.Web.Codes
{
    public class Md5Helper
    {
        /// <summary>
        /// 取得加密后的密码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetPasswordString(string str)
        {
            return GetMD5String("Web!" + str + "s_i-t~e");
        }
        /// <summary>
        /// 取得采用密钥加密后的密码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetKeyPasswordString(string str)
        {
            string passwordKey = Config.ConfigAdapter.SiteDataMgr.Groups["BaseSetting"].Settings["PasswordKey"].Value;
            if (passwordKey.Contains("$PD|"))
            {
                return GetMD5String(passwordKey.Replace("$PD|", str));
            }
            else
            {
                return GetMD5String(passwordKey + str);
            }
        }
        /// <summary>
        /// 取得字符串的MD5串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetMD5String(string str)
        {
            byte[] data = Encoding.GetEncoding("GB2312").GetBytes(str);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] OutBytes = md5.ComputeHash(data);

            string OutString = "";
            for (int i = 0; i < OutBytes.Length; i++)
            {
                OutString += OutBytes[i].ToString("x2");
            }
            return OutString.ToUpper();
        }
    }
}